---
title: "Homework 5"
author: "Alizeh Azhar"
date: "9/30/2021"
output:
  pdf_document: default
  html_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

# Problem 1: OIS Exercise 34.4a-e, page 142 (6 points)

In triathlons, it is common for racers to be placed into age and gender groups. Friends Leo and Mary both completed the Hermosa Beach Triathlon, where Leo competed in the Men, Ages 30 - 34 group while Mary competed in the Women, Ages 25 - 29 group. Leo completed the race in 1:22:28 (4948 seconds), while Mary completed the race in 1:31:53 (5513 seconds). Obviously Leo finished faster, but they are curious about how they did within their respective groups. Can you help them? Here is some information on the performance of their groups:

-   The finishing times of the Men, Ages 30 - 34 group has a mean of 4313 seconds with a standard deviation of 583 seconds.

-   The finishing times of the Women, Ages 25 - 29 group has a mean of 5261 seconds with a standard deviation of 807 seconds.

-   The distributions of finishing times for both groups are approximately Normal. Remember: a better performance corresponds to a faster finish.

(a) Write down the short-hand for these two normal distributions. (1 pt)\
    **Answer:**\
    For men: $Men\thicksim N(\mu=4313,\sigma=583)$\
    For women: $Women\thicksim N(\mu=5261, \sigma=807)$
(b) What are the Z-scores for Leo's and Mary's finishing times? What do these Z-scores tell you? (2 pts)\
    **Answer:**\
    Z-score for Leo: `r (4948-4313)/583`\
    Z-score for Mary: `r (5513-5261)/807`\
    Leo is `r (4948-4313)/583` standard deviations above the mean and Mary is `r (5513-5261)/807` standard deviation above the mean. So Mary did better in her group than Leo.
(c) Did Leo or Mary rank better in their respective groups? Explain your reasoning. (1 pt)\
    **Answer:**\
    Mary rank better than Leo. This is because Mary had a lower z- value than Leo. The lower the z-value the higher your rank is.
(d) What percent of the triathletes did Leo finish faster than in his group? (1 pt)\
    **Answer:**\

```{r}
pnorm(1.09,lower.tail=FALSE)
```

(e) What percent of the triathletes did Mary finish faster than in her group? (1 pt)\
    **Answer:**\

```{r}
pnorm(0.31,lower.tail=FALSE)
```

# Problem 2: OIS Exercise 4.13, page 148 (3 points)

A husband and wife both have brown eyes but carry genes that make it possible for their children to have brown eyes (probability 0.75), blue eyes (0.125), or green eyes (0.125).\
*Think very carefully about what distributions you should be using!*

a.  What is the probability the first blue-eyed child they have is their third child? Assume that the eye colors of the children are independent of each other. (1 pt)\
    **Answer:**\

```{r}
round(0.875^2*0.125,4)
```

b.  On average, how many children would such a pair of parents have before having a blue-eyed child? What is the standard deviation of the number of children they would expect to have until the first blue-eyed child? (2 pts)\
    **Answer:**\

```{r}
1/0.125
```

Mean ($\mu=$) `r 1/0.125`

```{r}
sqrt((1-0.125)/(0.125^2))
```

Standard deviation ($\sigma$)= `r round(sqrt((1-0.125)/(0.125^2)),4)`

# Problem 3: OIS Exercise 4.23, page 157 (6 pts)

Exercise 4.13 introduces a husband and wife with brown eyes who have 0.75 probability of having children with brown eyes, 0.125 probability of having children with blue eyes, and 0.125 probability of having children with green eyes.\
*Think very carefully about what distributions you should be using!*

(a) What is the probability that their first child will have green eyes and the second will not? (1 pt)\
    **Answer:**\

```{r}
dbinom(x=1, size=1, prob=0.125) * dbinom(x=1, size=1, prob=0.875)
```

(b) What is the probability that exactly one of their two children will have green eyes? (1 pt)\
    **Answer:**\

```{r}
dbinom(x=1, size=2, prob=0.125)
```

(c) If they have six children, what is the probability that exactly two will have green eyes? (1 pt)\
    **Answer:**\

```{r}
dbinom(x=2, size=6, prob=0.125)
```

(d) If they have six children, what is the probability that at least one will have green eyes? (1 pt)\
    **Answer:**\

```{r}
pbinom(q=1, size=6, prob=0.125, lower.tail=FALSE)
```

(e) What is the probability that the first green eyed child will be the 4th child? (1 pt)\
    **Answer:**\

```{r}
0.875^3 * 0.125^1
```

(f) Would it be considered unusual if only 2 out of their 6 children had brown eyes? (1 pt)\
    **Answer:**\

```{r}
pr <- pbinom(q=3, size=6, prob=0.75, lower.tail=FALSE)
if (pr > 0.5) {
  print(paste0("Yes ", pr))
} else {
  print(paste0("No", pr))
}
```

# Problem 4: OIS Exercise 4.32, page 165 (5 points)

A very skilled court stenographer makes one typographical error (typo) per hour on average.

(a) What probability distribution is most appropriate for calculating the probability of a given number of typos this stenographer makes in an hour? (1 pt)\
    **Answer:**\
    \emph{A Poisson distribution}: This is because the population is large (many keystrokes), and we are concerned with the number of events (counting typos).

(b) What are the mean and the standard deviation of the number of typos this stenographer makes? (2 pts)\
    **Answer:**\
    Since $\lambda=1$, the mean is **1** typo per hour and the standard deviation is $\sqrt{1}=1$ typo per hour

(c) Would it be considered unusual if this stenographer made 4 typos in a given hour? (1 pt)\
    **Answer:**\

```{r}
dpois(4, 1)
```

This indicates that there is only a 1.5% chance of 4 typos in 1 hour, therefore it would be considered unusual. Moreover, 4 is 3 standard deviations away from the mean of 1, so it is very unusual.

(d) Calculate the probability that this stenographer makes at most 2 typos in a given hour. (1 pt)\
    **Answer:**\

```{r}
ppois(2,1)
```

\newpage

# Problem 5 (5 points)

Suppose I work at a bakery that sells two types of pies: apple and pumpkin. The pies are boxed in such a way that I must open the box to identify what type of pie is inside. I know the probability that a randomly selected pie is an apple pie is 42%. Assume any samples are randomly selected, and assume that I am opening randomly selected boxes.

(a) How many boxes should I expect to open before I find an apple pie? (1 pt)\
    **Answer:**\

```{r}
1/0.42
```

(b) I have 27 pies. What is the probability that more than 17 are pumpkin? (1 pt)\
    **Answer:**\

```{r}
1- pbinom(17,27,0.58)
```

(c) What is the probability that I must open at least 5 boxes to find apple? (1 pt)\
    **Answer:**\

```{r}
1-pgeom(4,0.42)
```

(d) I have 32 pies to sell. How many should I expect to be apple? (1 pt)\
    **Answer:**\

```{r}
32*0.42
```

(e) Of the 32 pies, what is the probability that less than 9 are apple? (1 pt)\
    **Answer:**\

```{r}
pbinom(8,32,0.42)
```
